package com.xinchuang.service.targetSystem;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.CharUtil;
import cn.hutool.script.ScriptUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinchuang.comment.R;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.entity.target.TargetCommonAttribute;
import com.xinchuang.entity.target.TargetSpecialAttribute;
import com.xinchuang.entity.targetSystem.system.TargetSubType;
import com.xinchuang.entity.targetSystem.system.TargetType;
import com.xinchuang.entity.targetSystem.target.*;
import com.xinchuang.mapper.targetSystem.TargetMapper;
import com.xinchuang.mapper.targetSystem.TargetTypeMapper;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.target.TargetBaseDataService;
import com.xinchuang.service.target.TargetCommonAttributeService;
import com.xinchuang.service.target.TargetSpecialAttributeService;
import com.xinchuang.service.targetSystem.target.TargetMsgTableService;
import com.xinchuang.service.targetSystem.target.TargetSubMsgTableService;
import com.xinchuang.service.targetSystem.target.TargetSubRelationTableService;
import com.xinchuang.service.targetSystem.target.TargetSystemRelationService;
import com.xinchuang.util.FileElseUtil;
import com.xinchuang.util.LngLatUtil;
import com.xinchuang.util.SnowflakeIdWorker;
import com.xinchuang.utils.MyUtil;
import com.xinchuang.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/9 9:12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ExportUtil {

    private final TargetMapper targetMapper;

    private final TargetTypeMapper targetTypeMapper;

    private final TargetSubTypeService targetSubTypeService;

    private final TargetMsgTableService targetMsgTableService;

    private final TargetSubMsgTableService targetSubMsgTableService;

    private final TargetCommonAttributeService targetCommonAttributeService;

    private final TargetSpecialAttributeService targetSpecialAttributeService;

    private final TargetSubRelationTableService targetSubRelationTableService;

    private final TargetSystemRelationService targetSystemRelationService;

    private final KeyWordSystem keyWordSystem;

    private final RedisUseService redisUseService;

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(0, 0);

    @Transactional(rollbackFor = Exception.class)
    public R exportTarget(MultipartFile sourceFile) {
        Map<String, Object> target = null;
        List<Map<String, Object>> sub = new ArrayList<>();
        XWPFDocument document = null;
        try {
            document = new XWPFDocument(sourceFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return R.fail("文档解析异常，请检查文件后重试，请确认文件格式为docx");
        }
        List<XWPFTable> tables = document.getTables();
        for (int i = 0; i < tables.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            XWPFTable table = tables.get(i);
            int rows = table.getNumberOfRows();
            for (int k = 1; k < rows; k++) {
                XWPFTableRow row = table.getRow(k);
                int sign = 0;
                String key = "";
                String value = "";
                for (int j = 4; j >= 0; j--) {
                    XWPFTableCell rowCell = row.getCell(j);
                    if (null != rowCell) {
                        if (sign == 1) {
                            key = rowCell.getText();
                            map.put(key.trim(), value.trim());
                            break;
                        }
                        value = rowCell.getText();
                        sign++;
                    }
                }
            }
            if (i == 0) {
                target = map;
            } else {
                sub.add(map);
            }
        }
        //提取MB
        String[] targetStr = {"目标名称", "目标类别", "目标等级", "经度", "纬度", "国家地区（隶属）"};
        //MB类型
        Object typeName = target.get("上级类别");
        if (ObjectUtil.isEmpty(typeName)) {
            return R.fail("上级类别不存在，请在目标中添加后重试");
        }
        TargetType mainType = targetTypeMapper.selectOne(
                new LambdaQueryWrapper<TargetType>()
                        .eq(TargetType::getTypeName, typeName.toString())
                        .last("LIMIT 1")
        );
        String mainId;
        if (ObjectUtil.isEmpty(mainType)) {
            mainId = snow.nextId() + "";
            mainType = new TargetType();
            mainType.setTypeId(mainId);
            mainType.setParentId("1");
            mainType.setTypeName(typeName.toString());
            mainType.setLevel("1");
            Integer count = targetTypeMapper.selectSpecialCount("1");
            if (null == count) {
                mainType.setSort(1);
            } else {
                mainType.setSort(count + 1);
            }
            targetTypeMapper.insert(mainType);
        } else {
            mainId = mainType.getTypeId();
        }
        target.remove("上级类别");
        //获取并构建目标类型
        Object type = target.get("目标类别");
        if (ObjectUtil.isNotEmpty(type)) {
            TargetType selectOne = targetTypeMapper.selectOne(
                    new LambdaQueryWrapper<TargetType>()
                            .eq(TargetType::getParentId, mainId)
                            .eq(TargetType::getTypeName, type.toString())
            );
            if (ObjectUtil.isEmpty(selectOne)) {
                selectOne = new TargetType();
                selectOne.setTypeId(snow.nextId() + "");
                selectOne.setParentId(mainId);
                selectOne.setTypeName(type.toString());
                selectOne.setLevel("2");
                Integer count = targetTypeMapper.selectSpecialCount(mainId);
                if (null == count) {
                    selectOne.setSort(1);
                } else {
                    selectOne.setSort(count + 1);
                }
                //保存类型
                targetTypeMapper.insert(selectOne);
                targetTypeMapper.addSign(selectOne.getTypeId(), "default.png");
                redisUseService.updateTypeName(selectOne.getTypeId(), selectOne.getTypeName());
            }
            target.remove("目标类别");
            JSONArray arrayType = new JSONArray();
            arrayType.add(mainId);
            arrayType.add(selectOne.getTypeId());
            //构建目标基础数据
            TargetData targetData = getTargetData(targetStr, target, arrayType.toJSONString(), selectOne.getTypeId());
            //保存基础
            targetMapper.save(targetData);
            redisUseService.updateIsTarget(targetData.getTargetId());
            //系统类型归类
            targetSystemRelationService.save(
                    TargetSystemRelation.builder()
                            .targetId(targetData.getTargetId())
                            .targetClassify(targetData.getType())
                            .country(targetData.getCountry())
                            .build()
            );
            //目标基础数据
            List<TargetCommonAttribute> commonAttributes = targetCommonAttributeService.lambdaQuery().eq(TargetCommonAttribute::getSign, 1).list();
            List<TargetMsgTable> msgTables = getTargetBase(targetData, target, "1", commonAttributes);
            targetMsgTableService.saveBatch(msgTables);
            redisUseService.updateTargetBase(targetData.getTargetId(), msgTables);
            List<TargetSpecialAttribute> targetSpecials = getTargetSpecials(selectOne.getTypeId(), target);
            if (CollectionUtil.isNotEmpty(targetSpecials)) {
                targetSpecialAttributeService.saveOrUpdateBatch(targetSpecials);
                List<TargetMsgTable> targetMsgTables = getTargetSpecial(targetData.getTargetId(), "", targetSpecials, target);
                targetMsgTableService.saveBatch(targetMsgTables);
                redisUseService.updateTargetSpecial(targetData.getTargetId(), targetMsgTables);
            }
            System.out.println("----------------");

            //提取子目标
            String[] subStr = {"子目标名称", "经度", "纬度"};
            List<TargetSubRelationTable> subRelationTables = new ArrayList<>();
            for (Map<String, Object> map : sub) {
                String subId = snow.nextId() + "";
                //构建类型
                Object subType = map.get("子目标类别");
                String subTypeName = "";
                if (ObjectUtil.isNotEmpty(subType)) {
                    subTypeName = subType.toString();
                    map.remove("子目标类别");
                } else {
                    Object subName = map.get("子目标名称");
                    if (ObjectUtil.isEmpty(subName)) {
                        continue;
                    }
                    //提取名称中的类别
                    subTypeName = MyUtil.getStrChinese(subName.toString());
                }
                TargetSubType one = targetSubTypeService
                        .lambdaQuery()
                        .eq(TargetSubType::getParentId, targetData.getType())
                        .eq(TargetSubType::getTypeName, subTypeName)
                        .one();
                if (ObjectUtil.isEmpty(one)) {
                    one = new TargetSubType();
                    one.setTypeId(snow.nextId() + "");
                    one.setParentId(targetData.getType());
                    one.setTypeName(subTypeName);
                    one.setLevel("1");
                    redisUseService.updateSubTypeName(one.getTypeId(), one.getTypeName());
                    targetSubTypeService.save(one);
                }
                //解析基本信息
                List<TargetCommonAttribute> subCommonAttributes = targetCommonAttributeService.lambdaQuery().eq(TargetCommonAttribute::getSign, 2).list();
                List<TargetSubMsgTable> subBase = getSubBase(subStr, subId, "1", one.getTypeId(), map, subCommonAttributes);
                targetSubMsgTableService.saveBatch(subBase);
                redisUseService.updateSubBase(subId, subBase);
                redisUseService.updateIsSub(subId);
                List<TargetSpecialAttribute> subSpecials = getTargetSpecials(one.getTypeId(), map);
                if (CollectionUtil.isNotEmpty(subSpecials)) {
                    targetSpecialAttributeService.saveOrUpdateBatch(subSpecials);
                    List<TargetSubMsgTable> subSpecial = getSubSpecial(subId, "1", subSpecials, map);
                    targetSubMsgTableService.saveBatch(subSpecial);
                    redisUseService.updateSubSpecial(subId, subSpecial);
                }
                //目标的关联关系
                TargetSubRelationTable subRelationTable = new TargetSubRelationTable();
                subRelationTable.setTargetId(targetData.getTargetId());
                subRelationTable.setSubId(subId);
                subRelationTable.setPreType(1);
                subRelationTables.add(subRelationTable);
                redisUseService.updateTargetSubRelation(true, targetData.getTargetId(), subId);
            }
            if (CollectionUtil.isNotEmpty(subRelationTables)) {
                targetSubRelationTableService.saveBatch(subRelationTables);
            }
            return R.success("操作成功");
        }
        return R.fail("目标类别异常，请检查后重试");
    }

    private TargetData getTargetData(String[] targetStr, Map<String, Object> target, String type, String typeId) {
        double lng = 0.09887316937991386;
        double lat = 0.08726753962206146;
        String lngStr = "";
        String latStr = "";
        TargetData targetData = new TargetData();
        targetData.setTargetId(snow.nextId() + "");
        targetData.setTargetClassify(type);
        targetData.setType(typeId);
        for (String s : targetStr) {
            switch (s) {
                case "目标名称":
                    targetData.setTargetName(ObjectUtil.isNotEmpty(target.get(s)) ? target.get(s).toString() : "目标");
                    target.remove(s);
                    break;
                case "目标等级":
                    targetData.setGrade(ObjectUtil.isNotEmpty(target.get(s)) ? target.get(s).toString() : "");
                    target.remove(s);
                    break;
                case "国家地区（隶属）":
                    targetData.setCountry(ObjectUtil.isNotEmpty(target.get(s)) ? target.get(s).toString() : "");
                    target.remove(s);
                    break;
                case "经度":
                    lngStr = ObjectUtil.isNotEmpty(target.get(s)) ? target.get(s).toString() : "";
                    target.remove(s);
                    break;
                case "纬度":
                    latStr = ObjectUtil.isNotEmpty(target.get(s)) ? target.get(s).toString() : "";
                    target.remove(s);
                    break;
                default:
                    break;
            }
            if (StringUtils.isNotBlank(lngStr)) {
                double v = LngLatUtil.dfm2LatLng(lngStr, keyWordSystem.getDu(), keyWordSystem.getFen());
                if (v > 0) {
                    lng = v;
                }
            }
            if (StringUtils.isNotBlank(latStr)) {
                double v = LngLatUtil.dfm2LatLng(latStr, keyWordSystem.getDu(), keyWordSystem.getFen());
                if (v > 0) {
                    lat = v;
                }
            }
            targetData.setLon(lng);
            targetData.setLat(lat);
            targetData.setHeight(0D);
            targetData.setIndexValue("0");
            targetData.setRisk("0");
            targetData.setDeleteSign(1);
        }
        return targetData;
    }

    private List<TargetMsgTable> getTargetBase(TargetData targetData, Map<String, Object> map, String userId, List<TargetCommonAttribute> common) {
        List<TargetMsgTable> msgList = new ArrayList<>();
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("newimageUrl");
            targetMsg.setKeyName("图片");
            targetMsg.setKeyType("img");
            targetMsg.setKeyValue("[]");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("plotinfoobj");
            targetMsg.setKeyName("标绘信息");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("area");
            targetMsg.setKeyName("面积");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("0");
            targetMsg.setRemark("㎡");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("targetName");
            targetMsg.setKeyName("名称");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue(targetData.getTargetName());
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("targetClassify");
            targetMsg.setKeyName("类别");
            targetMsg.setKeyType("select");
            targetMsg.setKeyValue(targetData.getTargetClassify());
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("country");
            targetMsg.setKeyName("国家/地区");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue(targetData.getCountry());
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("grade");
            targetMsg.setKeyName("目标等级");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue(targetData.getGrade());
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("position");
            targetMsg.setKeyName("经纬度");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("[" + targetData.getLon() + "," + targetData.getLat() + ",0]");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("height");
            targetMsg.setKeyName("高程");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("");
            targetMsg.setRemark("m");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("riskLevel");
            targetMsg.setKeyName("危险程度");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("0");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("desicribe");
            targetMsg.setKeyName("描述");
            targetMsg.setKeyType("String");
            targetMsg.setKeyValue(ObjectUtil.isNotEmpty(map.get("概述")) ? map.get("概述").toString() : "");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
            map.remove("概述");
        }
        for (TargetCommonAttribute commonAttribute : common) {
            if (commonAttribute.getKeyValue().equals("desicribe")) {
                continue;
            }
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetData.getTargetId());
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId(commonAttribute.getKeyValue());
            targetMsg.setKeyName(commonAttribute.getKeyName());
            targetMsg.setKeyType(commonAttribute.getKeyType());
            targetMsg.setKeyValue("");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        return msgList;
    }

    private List<TargetSpecialAttribute> getTargetSpecials(String typeId, Map<String, Object> map) {
        List<TargetSpecialAttribute> specialAttributes = new ArrayList<>();
        if (CollectionUtil.isEmpty(map)) {
            return specialAttributes;
        }
        Map<String, Object> middle = new HashMap<>();
        middle.putAll(map);
        Set<String> typeNameSet = new HashSet<>();
        //剔除重复
        List<TargetSpecialAttribute> list = targetSpecialAttributeService.lambdaQuery().eq(TargetSpecialAttribute::getTypeId, typeId).list();
        if (CollectionUtil.isNotEmpty(list)) {
            for (TargetSpecialAttribute specialAttribute : list) {
                typeNameSet.add(specialAttribute.getKeyValue());
                if (null != middle.get(specialAttribute.getKeyName())) {
                    middle.remove(specialAttribute.getKeyName());
                    specialAttributes.add(specialAttribute);
                }
            }
        }

        int sign = 1;
        TargetSpecialAttribute specialAttribute;
        for (String s : middle.keySet()) {
            if (StringUtils.isBlank(s)) {
                continue;
            }
            specialAttribute = new TargetSpecialAttribute();
            specialAttribute.setTypeId(typeId);
            specialAttribute.setKeyName(s);
            specialAttribute.setKeyValue(getRandomName(typeNameSet));
            if (ObjectUtil.isNotEmpty(middle.get(s)) && middle.get(s).toString().length() > 10) {
                specialAttribute.setKeyType("String");
                specialAttribute.setShowType("textarea");
                int length = middle.get(s).toString().length() / 10;
                if (middle.get(s).toString().length() % 10 > 0) {
                    length += 1;
                }
                if (length > 8) {
                    length = 8;
                }
                specialAttribute.setRowValue(length + "");
            } else {
                if (FileElseUtil.isDouble(null != middle.get(s) ? middle.get(s).toString() : "")) {
                    specialAttribute.setKeyType("Double");
                } else {
                    specialAttribute.setKeyType("String");
                }
                specialAttribute.setShowType("text");
            }
            specialAttribute.setRequired("false");
            specialAttribute.setInfo("false");
            specialAttribute.setVerify("false");
            specialAttribute.setEnumerator("[]");
            specialAttribute.setSort(sign);
            specialAttributes.add(specialAttribute);
            sign++;
        }

        return specialAttributes;
    }

    private String getRandomName(Set<String> set) {
        String str = MyUtil.getRandomStr(8);
        if (set.contains(str)) {
            return getRandomName(set);
        }
        set.add(str);
        return str;
    }

    private List<TargetMsgTable> getTargetSpecial(String targetId, String userId, List<TargetSpecialAttribute> specialAttributes, Map<String, Object> map) {
        List<TargetMsgTable> msgList = new ArrayList<>();
        for (TargetSpecialAttribute commonAttribute : specialAttributes) {
            TargetMsgTable targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetId);
            targetMsg.setMsgType(2);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId(commonAttribute.getKeyValue());
            targetMsg.setKeyName(commonAttribute.getKeyName());
            targetMsg.setKeyType(commonAttribute.getKeyType());
            Object value = map.get(commonAttribute.getKeyName());
            targetMsg.setKeyValue(ObjectUtil.isNotEmpty(value) ? value.toString() : "");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        return msgList;
    }

    private List<TargetSubMsgTable> getSubBase(String[] subStr, String subId, String userId, String typeId, Map<String, Object> map, List<TargetCommonAttribute> commonAttributes) {
        List<TargetSubMsgTable> msgList = new ArrayList<>();
        {
            TargetSubMsgTable targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("newimageUrl");
            targetMsg.setKeyName("图片");
            targetMsg.setKeyType("img");
            targetMsg.setKeyValue("[]");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetSubMsgTable targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("plotinfoobj");
            targetMsg.setKeyName("标绘信息");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            TargetSubMsgTable targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("area");
            targetMsg.setKeyName("面积");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("0");
            targetMsg.setRemark("㎡");
            msgList.add(targetMsg);
        }
        {
            TargetSubMsgTable targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("targetClassify");
            targetMsg.setKeyName("类别");
            targetMsg.setKeyType("select");
            targetMsg.setKeyValue("[\"" + typeId + "\"]");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        double lng = 0.09887316937991386;
        double lat = 0.08726753962206146;
        String lngStr = "";
        String latStr = "";
        TargetSubMsgTable targetMsg;
        for (String s : subStr) {
            switch (s) {
                case "子目标名称":
                    targetMsg = new TargetSubMsgTable();
                    targetMsg.setSubId(subId);
                    targetMsg.setMsgType(1);
                    targetMsg.setPerson(userId);
                    targetMsg.setKeyId("targetName");
                    targetMsg.setKeyName("名称");
                    targetMsg.setKeyType("text");
                    targetMsg.setKeyValue(null != map.get(s) ? map.get(s).toString() : "子目标");
                    targetMsg.setRemark("");
                    msgList.add(targetMsg);
                    map.remove(s);
                    break;
                case "经度":
                    lngStr = ObjectUtil.isNotEmpty(map.get(s)) ? map.get(s).toString() : "";
                    map.remove(s);
                    break;
                case "纬度":
                    latStr = ObjectUtil.isNotEmpty(map.get(s)) ? map.get(s).toString() : "";
                    map.remove(s);
                    break;
                default:
                    break;
            }
        }
        if (StringUtils.isNotBlank(lngStr)) {
            double v = LngLatUtil.dfm2LatLng(lngStr, keyWordSystem.getDu(), keyWordSystem.getFen());
            if (v > 0) {
                lng = v;
            }
        }
        if (StringUtils.isNotBlank(latStr)) {
            double v = LngLatUtil.dfm2LatLng(latStr, keyWordSystem.getDu(), keyWordSystem.getFen());
            if (v > 0) {
                lat = v;
            }
        }
        {
            targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("position");
            targetMsg.setKeyName("经纬度");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("[" + lng + "," + lat + ",0]");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("height");
            targetMsg.setKeyName("高程");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("");
            targetMsg.setRemark("m");
            msgList.add(targetMsg);
        }
        {
            targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("riskLevel");
            targetMsg.setKeyName("危险程度");
            targetMsg.setKeyType("text");
            targetMsg.setKeyValue("0");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        {
            targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId("desicribe");
            targetMsg.setKeyName("描述");
            targetMsg.setKeyType("String");
            targetMsg.setKeyValue(ObjectUtil.isNotEmpty(map.get("概述")) ? map.get("概述").toString() : "");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
            map.remove("概述");
        }
        for (TargetCommonAttribute commonAttribute : commonAttributes) {
            if (commonAttribute.getKeyValue().equals("desicribe")) {
                continue;
            }
            targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(1);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId(commonAttribute.getKeyValue());
            targetMsg.setKeyName(commonAttribute.getKeyName());
            targetMsg.setKeyType(commonAttribute.getKeyType());
            targetMsg.setKeyValue("");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        return msgList;
    }

    private List<TargetSubMsgTable> getSubSpecial(String subId,
                                                  String userId,
                                                  List<TargetSpecialAttribute> specialAttributes,
                                                  Map<String, Object> map) {
        List<TargetSubMsgTable> msgList = new ArrayList<>();
        for (TargetSpecialAttribute commonAttribute : specialAttributes) {
            TargetSubMsgTable targetMsg = new TargetSubMsgTable();
            targetMsg.setSubId(subId);
            targetMsg.setMsgType(2);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId(commonAttribute.getKeyValue());
            targetMsg.setKeyName(commonAttribute.getKeyName());
            targetMsg.setKeyType(commonAttribute.getKeyType());
            Object value = map.get(commonAttribute.getKeyName());
            targetMsg.setKeyValue(ObjectUtil.isNotEmpty(value) ? value.toString() : "");
            targetMsg.setRemark("");
            msgList.add(targetMsg);
        }
        return msgList;
    }

}
